﻿using System;
using System.Drawing;
using System.Reflection;

namespace InkFx.WinControl.Core
{
    public static class ControlHelper
    {
        public static readonly BindingFlags RefBindingFlags = BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;


        /// <summary>
        /// 查找一个 控件 的 所属窗体
        /// </summary>
        public static System.Windows.Forms.Form FindOwneForm(System.Windows.Forms.Control control)
        {
            if (control == null) return null;
            return control.FindForm();
        }


        /// <summary>
        /// 查找 指定 父级控件 的所有子控件中, 递归得到了 第一个 T 类型控件
        /// </summary>
        public static T FindFirstRootControl<T>(System.Windows.Forms.Control parentControl) where T : System.Windows.Forms.Control
        {
            if (parentControl == null || parentControl.Controls == null) return null;

            foreach (System.Windows.Forms.Control childControl in parentControl.Controls)
                if (childControl is T) return (T)childControl;

            foreach (System.Windows.Forms.Control childControl in parentControl.Controls)
            {
                T tempControl = FindFirstRootControl<T>(childControl);
                if (tempControl != null) return tempControl;
            }

            return null;
        }

        /// <summary>
        /// 判断光标 是否在 指定的控件 区域内
        /// </summary>
        public static bool CursorInControl(System.Windows.Forms.Control control)
        {
            if (control == null || !control.Visible) return false;

            Point screenPoint = System.Windows.Forms.Cursor.Position;
            Rectangle rectangle = control.RectangleToScreen(control.ClientRectangle);  //RectangleToClient
            return rectangle.Contains(screenPoint);
        }

        /// <summary>
        /// 获取控件 的 屏幕区域
        /// </summary>
        public static Rectangle GetScreenRectangle(System.Windows.Forms.Control control)
        {
            if (control == null || !control.Visible) return Rectangle.Empty;

            Rectangle rectangle = control.RectangleToScreen(control.ClientRectangle);  //RectangleToClient
            return rectangle;
        }

        /// <summary>
        /// 判断屏幕坐标 是否在 指定区域内
        /// </summary>
        public static bool CursorInRectangle(Rectangle rectangle)
        {
            if (rectangle == Rectangle.Empty) return false;

            Point screenPoint = System.Windows.Forms.Cursor.Position;
            return rectangle.Contains(screenPoint);
        }
        /// <summary>
        /// 判断坐标 是否在 指定区域内
        /// </summary>
        public static bool PointInRectangle(Point point, Rectangle rectangle)
        {
            if (point == Point.Empty || rectangle == Rectangle.Empty) return false;
            return rectangle.Contains(point);
        }


        ///// <summary>
        ///// 触发一个 控件的 鼠标左键 按下事件
        ///// </summary>
        //public static void DoControlMouseDown(System.Windows.Forms.Control control)
        //{
        //    MouseEventArgs mouseDownEventArg = new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0);
        //    MethodInfo mouseDownMethod = typeof(Panel).GetMethod("OnMouseDown", ControlHelper.RefBindingFlags);
        //    mouseDownMethod.Invoke(control, new object[] { mouseDownEventArg });
        //}

        [Obsolete]
        public static void DoControlMouseDown(System.Windows.Forms.Control control)
        {
            IntPtr hWnd = control.Handle;
            const int WM_LBUTTONDOWN = 0x0201;

            Point screenPoint = System.Windows.Forms.Cursor.Position;
            Point clientPoint = control.PointToClient(screenPoint); //todo: 参考请用 Win32API.GetMouseCtrlPoint();

            int x = clientPoint.X;
            int y = clientPoint.Y;
            Win32API.SendMessage(hWnd, WM_LBUTTONDOWN, 1, (y * 65536 + x));
        }
        [Obsolete]
        public static void DoControlMouseUp(System.Windows.Forms.Control control)
        {
            IntPtr hWnd = control.Handle;
            const int WM_LBUTTONUP = 0x0202;

            //, int x, int y
            Point screenPoint = System.Windows.Forms.Cursor.Position;
            Point clientPoint = control.PointToClient(screenPoint); //todo: 参考请用 Win32API.GetMouseCtrlPoint();
            int x = clientPoint.X;
            int y = clientPoint.Y;
            Win32API.SendMessage(hWnd, WM_LBUTTONUP, 1, (y * 65536 + x));
        }
        public static void DoControlMouseDown(System.Windows.Forms.ToolStripItem control)
        {
            //const int WM_LBUTTONDOWN = 0x0201;

            //, int x, int y
            Point screenPoint = System.Windows.Forms.Cursor.Position;
            int x = screenPoint.X;
            int y = screenPoint.Y;

            System.Windows.Forms.MouseEventArgs mouseDownEventArg = new System.Windows.Forms.MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 1, x, y, 0);
            MethodInfo mouseDownMethod = typeof(System.Windows.Forms.ToolStripItem).GetMethod("OnMouseDown", ControlHelper.RefBindingFlags);
            mouseDownMethod.Invoke(control, new object[] { mouseDownEventArg });
        }

    }
}
